unit uEtiqUser;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, FMTBcd, DB, SqlExpr, Provider, DBClient, Grids,
  DBGrids, StdCtrls, Buttons, ACBrBase, ACBrETQ, ACBrDevice;

type
  TfEtiqUser = class(TForm)
    Panel1: TPanel;
    CDSuser: TClientDataSet;
    DSuser: TDataSource;
    DSPuser: TDataSetProvider;
    SQLuser: TSQLDataSet;
    SQLuserUSU_LOGUSU: TStringField;
    SQLuserUSU_NOMUSU: TStringField;
    CDSuserUSU_LOGUSU: TStringField;
    CDSuserUSU_NOMUSU: TStringField;
    ACBrETQ: TACBrETQ;
    SQLuserUSU_LOGUSU1: TStringField;
    CDSuserUSU_LOGUSU1: TStringField;
    sqlUser1: TSQLDataSet;
    dspUser1: TDataSetProvider;
    cdsUser1: TClientDataSet;
    dsUser1: TDataSource;
    sqlUser1USU_LOGUSU1: TStringField;
    sqlUser1USU_LOGUSU: TStringField;
    sqlUser1USU_NOMUSU: TStringField;
    cdsUser1USU_LOGUSU1: TStringField;
    cdsUser1USU_LOGUSU: TStringField;
    cdsUser1USU_NOMUSU: TStringField;
    Panel2: TPanel;
    Label1: TLabel;
    BitBtn1: TBitBtn;
    BitBtn7: TBitBtn;
    BitBtn5: TBitBtn;
    Panel3: TPanel;
    DBGrid2: TDBGrid;
    DBGrid1: TDBGrid;
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn7Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
    procedure DBGrid1TitleClick(Column: TColumn);
    procedure FormShow(Sender: TObject);
    procedure DBGrid2CellClick(Column: TColumn);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fEtiqUser: TfEtiqUser;

implementation

uses udm;

{$R *.dfm}

procedure TfEtiqUser.BitBtn1Click(Sender: TObject);
var str: string;
begin
  str := 'UPDATE F_USERS SET USU_APELID = SUBSTR(USU_APELID, 4, 20) '  +
    ' WHERE USU_APELID LIKE ' + QuotedStr('ATS%');
  dm.SQLConnection1.ExecuteDirect(str);
  cdsUser.Close;
  cdsUser.Open;
end;

procedure TfEtiqUser.BitBtn7Click(Sender: TObject);
var ordem , seg, strA : string;
    i,numero, multiplo  : integer;
    h : double;
    n : integer;
begin
  n := 2;
  if  MessageDlg('Imprimir Etiquetas Usuario ? ',
    mtConfirmation, [mbYes, mbNo],0) = mrNo then exit;

  if (not cdsUser.Active) then
    cdsUser.Open;

  cdsUser.first;

  i := 0;
  with ACBrETQ do
  begin
    Modelo := TACBrETQModelo(1);
    Porta := dm.PortaLpt;
    Avanco := 600 ;
    Ativar;
    Temperatura := 12;
    While (not cdsUser.Eof) do
    begin
      numero := i;                       //altura - Largura
      ImprimirTexto(orNormal, 1, n, n, 5, 0110,(CDSuserUSU_NOMUSU.AsString));
      ImprimirBarras(orNormal, 'E', '2', '2', 30, 060, CDSuserUSU_LOGUSU1.AsString, 110);  // Esta imprime correto  PERFEITO

      if (not cdsUser.Eof) then
      cdsUser.Next;
      // ETIQUETA 2
      ImprimirTexto(orNormal, 1, n, n, 5, 0510,(CDSuserUSU_NOMUSU.AsString));
      ImprimirBarras(orNormal, 'E', '2', '2', 30, 480, CDSuserUSU_LOGUSU1.AsString, 110);  // Esta imprime correto  PERFEITO

      Imprimir(StrToInt('1')); // Numero de Copias
      i := i + 2;

      //Evitando o estouro do Buffer da impressora.
      multiplo := i mod 5;
      if multiplo = 0 then
      begin
        //cont := 0;
        Sleep(1500);
        Application.ProcessMessages;
      end;
      cdsUser.Next;
    end;
    Desativar;
  end;
end;

procedure TfEtiqUser.BitBtn5Click(Sender: TObject);
begin
  close;
end;

procedure TfEtiqUser.DBGrid1TitleClick(Column: TColumn);
begin
  CDSuser.IndexFieldNames := Column.FieldName;
end;

procedure TfEtiqUser.FormShow(Sender: TObject);
begin
  if (not cdsUser.Active) then
    cdsUser.Open;
  if (not cdsUser1.Active) then
    cdsUser1.Open;
end;

procedure TfEtiqUser.DBGrid2CellClick(Column: TColumn);
var str: string;
begin
  if (not cdsUser.Active) then
    cdsUser.Open;

  if (not cdsUser.Locate('USU_LOGUSU', cdsUser1USU_LOGUSU.asString, [loCaseInsensitive])) then
  begin
    str := 'UPDATE F_USERS SET USU_APELID = ' +  QuotedStr('ATS') + ' || USU_APELID ' +
      ' WHERE USU_LOGUSU = ' + QuotedStr(cdsUser1USU_LOGUSU.asString);
    dm.SQLConnection1.ExecuteDirect(str);
    if (cdsUser.Active) then
      cdsUser.Close;
    cdsUser.Open;
  end;

  Label1.Caption := IntToStr(cdsUser.RecordCount) + ' Registros selecionados';

end;

procedure TfEtiqUser.FormClose(Sender: TObject; var Action: TCloseAction);
var str: string;
begin
  str := 'UPDATE F_USERS SET USU_APELID = SUBSTR(USU_APELID, 4, 20) '  +
    ' WHERE USU_APELID LIKE ' + QuotedStr('ATS%');
  dm.SQLConnection1.ExecuteDirect(str);
end;

end.
